package com.uphone.kingmall.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.base.BaseActivity;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI iwxapi;
    public static BaseActivity activity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.e("WXPayEntryActivity");
        iwxapi = MyApplication.api;
        iwxapi.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        iwxapi.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }


    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            LogUtils.e("\n微信transaction：" + resp.transaction + "\n微信errStr：" + resp.errStr +
                    "\n微信openId：" + resp.openId + "\n微信errCode：" + resp.errCode);
            if (resp.errCode == 0) {
                // 微信支付成功
                if (activity != null) {
                    activity.wxPaySuccess();
                }
            } else if (resp.errCode == -2) {
                ToastUtils.showShort("取消支付！");
            } else {
                ToastUtils.showShort("支付失败！");
            }
        }
        finish();
    }

}